home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Lines Curves and Area Fills / PrintableForm / PrintableForm.cs next >
Encoding:
Text File  |  2001-01-15  |  1.5 KB  |  51 lines

  1. //--------------------------------------------
  2. // PrintableForm.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8.  
  9. class PrintableForm: Form
  10. {
  11.      public static void Main()
  12.      {
  13.           Application.Run(new PrintableForm());
  14.      }
  15.      public PrintableForm()
  16.      {
  17.           Text = "Printable Form";
  18.           BackColor = SystemColors.Window;
  19.           ForeColor = SystemColors.WindowText;
  20.           ResizeRedraw = true;
  21.      }
  22.      protected override void OnPaint(PaintEventArgs pea)
  23.      {
  24.           DoPage(pea.Graphics, ForeColor, 
  25.                  ClientSize.Width, ClientSize.Height);
  26.      }
  27.      protected override void OnClick(EventArgs ea)
  28.      {
  29.           PrintDocument prndoc = new PrintDocument();
  30.  
  31.           prndoc.DocumentName = Text;
  32.           prndoc.PrintPage +=
  33.                  new PrintPageEventHandler(PrintDocumentOnPrintPage);
  34.           prndoc.Print();
  35.      }
  36.      void PrintDocumentOnPrintPage(object obj, PrintPageEventArgs ppea)
  37.      {
  38.           Graphics grfx  = ppea.Graphics;
  39.           SizeF    sizef = grfx.VisibleClipBounds.Size;
  40.  
  41.           DoPage(grfx, Color.Black, (int)sizef.Width, (int)sizef.Height);
  42.      }
  43.      protected virtual void DoPage(Graphics grfx, Color clr, int cx, int cy)
  44.      {
  45.           Pen pen = new Pen(clr);
  46.  
  47.           grfx.DrawLine(pen, 0,      0, cx - 1, cy - 1);
  48.           grfx.DrawLine(pen, cx - 1, 0, 0,      cy - 1);
  49.      }
  50. }
  51.